package outman.weibo.model;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;

import android.util.Log;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import outman.weibo.Weibo;
import outman.weibo.WeiboClient;
import outman.weibo.WeiboParameter;
import outman.weibo.util.HttpUtil;

public class User {

    private static final String TAG = "User";

    public static final String USER_SHOW = Weibo.BASE_URL + "users/show.json";

    private String id;
    private String name;

    public String getId() {
	return id;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public User(String uid, String accessToken) {
	initFrom(uid, accessToken);
	this.id = uid;
    }

    private void initFrom(String uid, String accessToken) {
	WeiboParameter parameter = new WeiboParameter();
	parameter.add("uid", uid);
	parameter.add(WeiboClient.ACCESS_TOKEN, accessToken);
	HttpGet get = new HttpGet(USER_SHOW + parameter.toGetParam());

	String userJson = HttpUtil.doGet(get);

	Log.i(TAG, userJson);

	JsonFactory factory = new JsonFactory();
	try {
	    JsonParser parser = factory.createJsonParser(userJson);

	    while (JsonToken.END_OBJECT != parser.nextToken()) {
		if ("screen_name".equals(parser.getText())) {
		    parser.nextToken();
		    this.name = parser.getText();
		    return;
		}
	    }

	} catch (JsonParseException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}

    }
}
